package com.crm.service.client.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.crm.dao.client.ContactDao;
import com.crm.dao.client.MemorialDao;
import com.crm.pojo.Contact;
import com.crm.pojo.Memorial;
import com.crm.service.client.MemorialService;
import com.crm.vo.MemorialVo;
import com.crm.vo.Result;
@Service
@Transactional(rollbackFor = Exception.class)
public class MemorialServiceImpl implements MemorialService{

	@Autowired
	private MemorialDao memorialDao;
	@Autowired
	private ContactDao contactDao;
	
	@Override
	public List<MemorialVo> queryAllMemorial(int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		List<MemorialVo> list = memorialDao.queryAllMemorial(page);
		return list;
	}

	@Override
	public Long queryCountAllMemorial() {
		return memorialDao.queryCountAllMemorial();
	}

	@Override
	public List<MemorialVo> queryMemorialByMemId(Long memId, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return memorialDao.queryMemorialByMemId(memId, page);
	}

	@Override
	public Long queryCountMemorialByMemId(Long memId) {
		return memorialDao.queryCountMemorialByMemId(memId);
	}

	@Override
	public List<MemorialVo> queryMemorialByContactName(String contactName, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return memorialDao.queryMemorialByContactName("%"+contactName+"%", page);
	}

	@Override
	public Long queryCountMemorialByContactName(String contactName) {
		return memorialDao.queryCountMemorialByContactName("%"+contactName+"%");
	}

	@Override
	public List<MemorialVo> queryMemorialCurMonth() {
		return memorialDao.queryMemorialCurMonth();
	}

	@Override
	public List<MemorialVo> queryMemorialNextMonth() {
		return memorialDao.queryMemorialNextMonth();
	}

	@Override
	public Result addMemorial(Long memId, BigDecimal contactid, String type, Date memDate, String remarks) {
		
		try {
			Memorial memorial;
			if(memId != null) {
				memorial = memorialDao.findOne(memId);
			}else {
				memorial = new Memorial();
			}
			memorial.setClientId(contactid);
			memorial.setCreateDate(new Date());
			memorial.setMemDate(memDate);
			memorial.setMemorialIsDel("否");
			memorial.setRemarks(remarks);
			memorial.setType(type);
			Contact contact = contactDao.findOne(contactid.longValue());
			memorial.setAcceptUserId(contact.getClientInfo().getUserId());
			memorialDao.save(memorial);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，添加联系人纪念日失败！");
		}
	}

	@Override
	public Result delMemorial(Long memId) {
		if(memId == null) return new Result("亲，您还没有选择联系人");
		Memorial memorial = memorialDao.findOne(memId);
		memorial.setMemorialIsDel("是");
		memorialDao.save(memorial);
		return Result.SUCCESS;
	}

	

	

}
